/*
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//      Copyright (c) 2006-2007 Intel Corporation. All Rights Reserved.
//
//
*/
#include "umc_defs.h"
#if defined (UMC_ENABLE_DVHD_VIDEO_DECODER)

#include "umc_dv100_decoder.h"
#include <ippvc.h>

namespace UMC
{

static Ipp32s MacroBlockWidth = 16;
static Ipp32s MacroBlockHeight = 16;
static Ipp32s MacroBlockHalfHeight = 8;
static Ipp32s SuperBlockWidth = 6 * MacroBlockWidth;

static
inline
Ipp32s GetMacroBlockOffsetInADevidedBlock(Ipp32s i, Ipp32s k, Ipp32s Pitch, Ipp32s BytesPerPixel)
{
    Ipp32s Row, Col;
    if(i%2 == 0)
    {
        Row = k / 6;
        Col = k % 6;
        return (i/2) * 9 * MacroBlockHeight * Pitch +
                Row * MacroBlockHeight * Pitch +
                Col * MacroBlockWidth * BytesPerPixel;
    }
    else
    {
        Row = (k + 3) / 6;
        Col = (k + 3) % 6;
        return ( (i/2) * 9 * MacroBlockHeight + MacroBlockHeight * 4 ) * Pitch +
                Row * MacroBlockHeight * Pitch  +
                Col * MacroBlockWidth * BytesPerPixel;
    }
}

static
void InitializeOffsets(Ipp8u **pDestinations, Ipp8u *pDestImage, size_t nDestPitch, Ipp32s nChannelNum, Ipp32s nDIFSeqNum, Ipp32s nVideoSegmentNum)
{
    Ipp32s MBNum;
    Ipp32s s = nDIFSeqNum / 5;
    Ipp32s x = nDIFSeqNum % 5;
    Ipp32s t = (nVideoSegmentNum + 27*x) % 5;
    Ipp32s k = (nVideoSegmentNum + 27*x) / 5;
    Ipp32s h = nChannelNum;
    Ipp32s SuperBlockVertOrder[]=
    {
        (4*h + s + 2*t + 2) % 10, // a
        (4*h + s + 2*t + 6) % 10, // b
        (4*h + s + 2*t + 8) % 10, // c
        (4*h + s + 2*t + 0) % 10, // d
        (4*h + s + 2*t + 4) % 10  // e
    };
    Ipp32s SuperBlockHorzOrder[] = {2, 1, 3, 0, 4};
    Ipp32s BytesPerPixel = 2;

    if(h>=2) h-=2;

    for(MBNum = 0; MBNum<5; MBNum++)
    {
        pDestinations[2*MBNum] = pDestImage +
                         (SuperBlockHorzOrder[MBNum]*2 + h) * SuperBlockWidth * BytesPerPixel +
                         GetMacroBlockOffsetInADevidedBlock(SuperBlockVertOrder[MBNum], k, nDestPitch, BytesPerPixel);

        pDestinations[2*MBNum+1] = pDestinations[2*MBNum] + MacroBlockHalfHeight * nDestPitch;
    }
}


void DV100VideoDecoder::Store_System720_60p_Segment(Ipp16s* pDecodedVideoSegment, Ipp32s nChannelNum, Ipp32s nDIFSeqNum, Ipp32s nVideoSegmentNum)
{
    Ipp8u *pDestinations[10];
    // reset pointers
    if (0 == m_shortCutModeFlag)
    {
        InitializeOffsets(pDestinations, m_pDestination, m_nDestPitch, nChannelNum, nDIFSeqNum, nVideoSegmentNum);
        ippiYCrCb422ToYCbCr422_10HalvesMB16x8_DV100_16s8u_P3C2R(pDecodedVideoSegment, pDestinations, m_nDestPitch);
    }
    else if (1 == m_shortCutModeFlag)
    {
        MacroBlockWidth = 8;
        MacroBlockHeight = 8;
        MacroBlockHalfHeight = 4;
        SuperBlockWidth = 6 * MacroBlockWidth;
        InitializeOffsets(pDestinations, m_pDestination, m_nDestPitch, nChannelNum, nDIFSeqNum, nVideoSegmentNum);
        QuarterResolutionStoreYCrCb422ToYCbCr422_DV100_16s8u_P3C2R(pDecodedVideoSegment, pDestinations, m_nDestPitch);
    }
    else if (2 == m_shortCutModeFlag)
    {
        MacroBlockWidth = 4;
        MacroBlockHeight = 4;
        MacroBlockHalfHeight = 2;
        SuperBlockWidth = 6 * MacroBlockWidth;
        InitializeOffsets(pDestinations, m_pDestination, m_nDestPitch, nChannelNum, nDIFSeqNum, nVideoSegmentNum);
        Resolution8x8To4x4To2x2StoreYCrCb422ToYCbCr422_DV100_16s8u_P3C2R(pDecodedVideoSegment, pDestinations, m_nDestPitch);
    }
    else if (3 == m_shortCutModeFlag)
    {
        MacroBlockWidth = 2;
        MacroBlockHeight = 2;
        MacroBlockHalfHeight = 1;
        SuperBlockWidth = 6 * MacroBlockWidth;
        InitializeOffsets(pDestinations, m_pDestination, m_nDestPitch, nChannelNum, nDIFSeqNum, nVideoSegmentNum);
        DCOnlyStoreYCrCb422ToYCbCr422_DV100_16s8u_P3C2R(pDecodedVideoSegment, pDestinations/*, m_nDestPitch*/);
    }
    else if (4 == m_shortCutModeFlag)
    {
        MacroBlockWidth = 16;
        MacroBlockHeight = 8;
        MacroBlockHalfHeight = 4;
        SuperBlockWidth = 6 * MacroBlockWidth;
        InitializeOffsets(pDestinations, m_pDestination, m_nDestPitch, nChannelNum, nDIFSeqNum, nVideoSegmentNum);
        Resolution8x8To8x4StoreYCrCb422ToYCbCr422_DV100_16s8u_P3C2R(pDecodedVideoSegment, pDestinations, m_nDestPitch);
    }
    else if (5 == m_shortCutModeFlag)
    {
        MacroBlockWidth = 16;
        MacroBlockHeight = 4;
        MacroBlockHalfHeight = 2;
        SuperBlockWidth = 6 * MacroBlockWidth;
        InitializeOffsets(pDestinations, m_pDestination, m_nDestPitch, nChannelNum, nDIFSeqNum, nVideoSegmentNum);
        Resolution8x8To8x2StoreYCrCb422ToYCbCr422_DV100_16s8u_P3C2R(pDecodedVideoSegment, pDestinations, m_nDestPitch);
    }
 }

}//namespace UMC

#endif //(UMC_ENABLE_DVHD_VIDEO_DECODER)
